package demo.practice.bfs;

import java.util.*;

public class P127_2 {

    public static void main(String[] args) {
        P127_2 p127 = new P127_2();
        int i = p127.ladderLength("hit", "cog", Arrays.asList("hot", "dot", "dog", "lot", "log", "cog"));
        System.out.println(i);

    }

    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        if (beginWord == null || endWord == null || beginWord.length() != endWord.length() || beginWord.equals(endWord))
            return 0;

        Trie t = new Trie();

        for (String s : wordList) {
            if (s.length() != beginWord.length())
                continue;
            char[] chars = s.toCharArray();
            for (int i = 0; i < s.length(); i++) {
                char o = chars[i];
                chars[i] = '*';
                t.insert(chars, s);
                chars[i] = o;
            }
        }

        Queue<String> queue = new LinkedList<>();
        queue.add(beginWord);

        Set<String> visited = new HashSet<>();
        visited.add(beginWord);

        int level = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size > 0) {
                String top = queue.poll();
                if (top.equals(endWord))
                    return level + 1;
                else {
                    Set<String> regexs = t.regex(top);
                    for (String regex : regexs) {
                        if (!visited.contains(regex)) {
                            queue.add(regex);
                            visited.add(regex);
                        }
                    }
                }
                size--;
            }
            level++;
        }
        return 0;
    }

    class Trie {
        Map<Character, Trie> child = new HashMap<>();
        Set<String> words;
        Set<String> books = new HashSet<>();

        public void insert(char[] wordChars, String oldword) {
            Trie cur = this;
            for (char c : wordChars) {
                if (cur.child.get(c) == null) {
                    cur.child.put(c, new Trie());
                }
                cur = cur.child.get(c);
            }
            if (cur.words == null) {
                cur.words = new HashSet<>();
            }
            cur.words.add(oldword);
        }

        public Set<String> regex(String word) {
            Set<String> set = new HashSet<>();

            char[] chars = word.toCharArray();
            for (int i = 0; i < word.length(); i++) {
                char o = chars[i];
                chars[i] = '*';
                Set<String> words = getWords(chars);
                if (words != null && words.size() > 0)
                    set.addAll(words);
                chars[i] = o;
            }
            set.remove(word);
            return set;
        }

        private Set<String> getWords(char[] wordChars) {
            Trie cur = this;
            for (char c : wordChars) {
                if (cur.child.get(c) == null) {
                    return null;
                } else {
                    cur = cur.child.get(c);
                }
            }
            return cur.words;
        }
    }

}
